﻿using EasyCore.DependencyInjection.Interfaces;
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace EasyCore.DataValidation
{
    /// <summary>
    /// 验证器的扩展方法
    /// </summary>
    public static class EasyAdminValidationExtensions
    {
        public static void AddDataValidators(this IServiceCollection services)
        {
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();//获得所有的程序集
            var errectiveTypes = assemblies.SelectMany(x => x.GetTypes());//获得所有的类型
            var canInjectTypes = errectiveTypes.Where(x => typeof(EasyAdminValidator<>).IsAssignableFrom(x) && x.IsClass && !x.IsAbstract && !x.IsInterface);//获得能依赖注入的类型
            foreach (var type in canInjectTypes)
            {
                services.AddValidatorsFromAssemblyContaining(type);//注册相应的类型
            }
        }
    }
}
